package com.lightjet.macross.base.upms.service.impl;

import com.lightjet.macross.base.feign.api.domain.AuthUser;
import com.lightjet.macross.base.upms.domain.SysOrganization;
import com.lightjet.macross.base.upms.domain.SysUser;
import com.lightjet.macross.base.upms.repository.SysUserRepository;
import com.lightjet.macross.base.upms.service.SysOrganizationService;
import com.lightjet.macross.base.upms.service.SysUserService;
import com.lightjet.macross.common.core.BaseServiceImpl;
import com.lightjet.macross.common.util.MD5Util;
import io.netty.util.internal.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;

import javax.annotation.Resource;
import java.util.List;

@Service("sysUserServiceImpl")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser,String> implements SysUserService {

    @Resource
    SysOrganizationService sysOrganizationService;

    @Override
    public SysUser FromAuthUser(AuthUser authUser) {
        SysUser userView = new SysUser();
        userView.setDisplayname(authUser.getDisplayname());
        userView.setEmail(authUser.getEmail());
        if(!StringUtil.isNullOrEmpty(authUser.getOrgid())){
            SysOrganization sysOrganization = sysOrganizationService.FindDistinctById(authUser.getOrgid());
            if(sysOrganization!=null)
            {
                userView.setAffiliation(sysOrganization);
            }
        }
        userView.setAffiliation(new SysOrganization());
        userView.setUsername(authUser.getUsername());
        userView.setUnicode(authUser.getUnicode());
        userView.setWorkno(authUser.getWorkno());
        userView.setId(authUser.getId());

        return userView;
    }

    @Override
    public AuthUser ToAuthUser(SysUser sysUser) {
        AuthUser authUser = new AuthUser();
        authUser.setDisplayname(sysUser.getDisplayname());
        authUser.setId(sysUser.getId());
        authUser.setUsername(sysUser.getUsername());
        authUser.setEmail(sysUser.getEmail());
        authUser.setOrgid(sysUser.getAffiliation()== null?"-1":sysUser.getAffiliation().getId());
        authUser.setOrgname(sysUser.getAffiliation()== null?"-1":sysUser.getAffiliation().getDisplayname());
        authUser.setPassword(sysUser.getPassword());
        authUser.setWorkno(sysUser.getWorkno());
        authUser.setUnicode(sysUser.getUnicode());
        authUser.setUsibn("macross");
        return authUser;
    }

    @Override
    public SysUser FindDistinctById(String id) {
        return ((SysUserRepository)baseRepository).findDistinctById(id);
    }

    @Override
    public SysUser FindDistinctByUsername(String username) {
        return ((SysUserRepository)baseRepository).findDistinctByUsername(username);
    }

    @Override
    public Page<SysUser> FindPagedByFilters(String username, String displayname, String workno, String deptid, Pageable pageable) {
        return ((SysUserRepository)baseRepository).findPagedByFilters(username,displayname,workno,deptid,pageable);
    }

    @Override
    public SysUser Create(SysUser user) {
        user.setSalt(java.util.UUID.randomUUID().toString());
        user.setPassword(MD5Util.Encode(user.getPassword(), user.getSalt()));//这里因为password被json设为json忽略 所以从前端传来的password实际放在reserved2字段
        if(user.getUserstate() == null){
            user.setUserstate(SysUser.UserStateEnum.NORMAL);
        }
        return this.CreateAndFlush(user);
        /*try {
            this.CreateAndFlush(user);
            return ResultDTO.CreateOKResult();
        }
        catch (DataIntegrityViolationException e)
        {
            String msg = e.getMessage();
            if(e.getCause() instanceof ConstraintViolationException) {
                msg = MessageFormat.format("新增用户失败：用户名【{0}】重复", user.getUsername());
            }
            return ResultDTO.CreateErrorResult(msg);
        }
        catch (Exception e)
        {
            return ResultDTO.CreateErrorResult(e.getMessage());
        }*/
    }

    /**
     * Description //根据筛选条件查询用户
     *
     * @param username
     * @param displayname 显示名称
     * @param email       邮件地址
     * @param mobile      移动电话
     * @return java.util.List<com.lightjet.macross.base.upms.domain.SysUser>
     * CreateTime 2020/3/9 10:57
     * @author lijia
     * @Param username 用户名
     **/
    @Override
    public List<SysUser> FindByFilters(String username, String displayname, String email, String mobile) {
        return ((SysUserRepository)baseRepository).findAllByFilter(username,displayname,email,mobile);
    }

    @Override
    public Page<SysUser> FindPageByKeyWords(String keywords, Pageable pageable) {
        return ((SysUserRepository)baseRepository).findPageByKeyWords(keywords,pageable);
    }
}
